package com.bls.cate.ui.activity.person_other;

import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.widget.NestedScrollView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bls.blslib.base.BaseActivity;
import com.bls.blslib.eventbus.Event;
import com.bls.blslib.eventbus.EventBusUtils;
import com.bls.cate.R;
import com.bls.cate.constant.Constant;
import com.bls.cate.constant.EventBusCode;
import com.bls.cate.constant.Interface;
import com.bls.cate.listener.AppBarStateChangeListener;
import com.bls.cate.model.AttentionListRes;
import com.bls.cate.model.RootRes;
import com.bls.cate.model.SearchUserRes;
import com.bls.cate.ui.widget.POMPopWindow;
import com.bls.cate.ui.widget.TitleView;
import com.bls.cate.utils.AccountUtil;
import com.bls.cate.utils.FrescoImageLoader;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.Objects;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/19.
 */

public class PersonOtherActivity extends BaseActivity implements AppBarStateChangeListener.OnStateChangeListener, TitleView.OnTitleClickListener, POMPopWindow.POMPopWinListener {

    private AppBarLayout appBar;
    private TitleView titleView;
    private CollapsingToolbarLayout ctlTop;
    private NestedScrollView nsvBottom;
    private TextView btnChat, btnLetter, btnMore;
    private SimpleDraweeView ivHeader;
    private TextView tvName;
    private SimpleDraweeView ivImage;

    private PersonOtherActivity mActivity;
    private int otherIntId;
    private String otherUniqueId, otherNickName, otherHeaderImg;
    private String uniqueId;
    private boolean isAttention = false;
    private POMPopWindow pomPopWindow;


    @Override
    protected void initParams() {
        super.initParams();
        mActivity = (PersonOtherActivity) getActivity();
        uniqueId = AccountUtil.getUniqueId();
        String fromPage = getIntent().getStringExtra("fromPage");
        if (Objects.equals(fromPage, "SearchUserActivity")) {
            SearchUserRes.ResultListDataBean searchUserResUserInfo = (SearchUserRes.ResultListDataBean) getIntent().getSerializableExtra("resultListDataBean");
            otherIntId = searchUserResUserInfo.getIntId();
            otherUniqueId = searchUserResUserInfo.getUniqueId();
            otherNickName = searchUserResUserInfo.getNickName();
            otherHeaderImg = searchUserResUserInfo.getHeaderImgMiddle();
        } else if (Objects.equals(fromPage, "AttentionActivity")) {
            AttentionListRes.ResultListDataBean baseUserInfo = (AttentionListRes.ResultListDataBean) getIntent().getSerializableExtra("resultListDataBean");
            otherIntId = baseUserInfo.getOtherIntId();
            otherUniqueId = baseUserInfo.getOtherUniqueId();
            otherNickName = baseUserInfo.getOtherNickName();
            otherHeaderImg = baseUserInfo.getOtherHeaderImgMiddle();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_person_other;
    }

    @Override
    protected void initFvb() {
        super.initFvb();
        titleView = findViewById(R.id.title_view);
        appBar = findViewById(R.id.app_bar);
        ctlTop = findViewById(R.id.ctl_top);
        ivHeader = findViewById(R.id.iv_header);
        tvName = findViewById(R.id.tv_name);
        nsvBottom = findViewById(R.id.nsv_bottom);
        btnChat = findViewById(R.id.btn_chat);
        btnLetter = findViewById(R.id.btn_letter);
        btnMore = findViewById(R.id.btn_more);
        ivImage = findViewById(R.id.iv_image);

    }

    @Override
    protected void initTitle() {
        titleView.setTitleViewBg(mActivity.getResources().getColor(R.color.colorTrans));
        titleView.titleBottomLine.setVisibility(View.GONE);
        titleView.titleLeftIv1.setVisibility(View.VISIBLE);
        titleView.titleRightIv.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initView() {
        super.initView();
        btnChat.setText("发私信");
        btnLetter.setText("写情书");
        btnMore.setText("更多");
        tvName.setText(otherNickName);
        FrescoImageLoader.showImage(ivHeader, otherHeaderImg);
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setAutoPlayAnimations(true)
                .setUri(Uri.parse("res://" + AppUtils.getAppPackageName() + "/" + R.drawable.temp_gif))
                .build();
        ivImage.setController(draweeController);
    }

    @Override
    protected void initListener() {
        AppBarStateChangeListener appBarStateChangeListener = new AppBarStateChangeListener(mActivity, this);
        appBar.addOnOffsetChangedListener(appBarStateChangeListener);
        titleView.setTitleClickListener(this);
        setOnClickListener(ivHeader, btnChat, btnLetter, btnMore);
    }

    @Override
    protected void initData() {
        super.initData();
        presenter.requestStrData(0, Interface.INTERFACE_ATTENTION, CacheMode.NO_CACHE,
                new HttpParams("RequestMethod", "AttentionIsTrue"),
                new HttpParams("UniqueId", uniqueId),
                new HttpParams("OtherUniqueId", otherUniqueId));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_header:
                ToastUtils.showShort(R.string.to_be_continued);
                break;
            case R.id.btn_chat:

                break;
            case R.id.btn_letter:
                ToastUtils.showShort(R.string.to_be_continued);
            case R.id.btn_more:
                View rootView = LayoutInflater.from(mActivity).inflate(R.layout.activity_person_other, null);
                pomPopWindow = new POMPopWindow(mActivity, this);
                pomPopWindow.setAttentionStatue(isAttention);
                pomPopWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
                break;
        }
    }

    @Override
    public void onStateChanged(AppBarLayout appBarLayout, AppBarStateChangeListener.State state) {
        setTitleState(state == AppBarStateChangeListener.State.COLLAPSED);
    }

    /**
     * 设置title状态
     *
     * @param isCollapsed 是否处于折叠状态
     */
    private void setTitleState(boolean isCollapsed) {
        titleView.titleMiddleText.setText(isCollapsed ? otherNickName : "");
        titleView.titleLeftIv1.setImageResource(isCollapsed ? R.mipmap.icon_arrow_1 : R.mipmap.icon_arrow_1_white);
        titleView.titleRightIv.setImageResource(isCollapsed ? R.mipmap.icon_more_black : R.mipmap.icon_more_white);
        titleView.setTitleViewBg(isCollapsed ? mActivity.getResources().getColor(R.color.colorWhite) : mActivity.getResources().getColor(R.color.colorTrans));
    }

    @Override
    public void onTitleClick(int btnType) {
        switch (btnType) {
            case 1:
                mActivity.finish();
                break;
            case 3:
                ToastUtils.showShort(R.string.to_be_continued);
                break;
        }
    }

    @Override
    public void onPOMPopWinBtn(Constant.POMPopWinEnum pomPopWinEnum, boolean isAttention) {
        if (pomPopWinEnum == Constant.POMPopWinEnum.ATTENTION && isAttention) {
            presenter.requestStrData(1, Interface.INTERFACE_ATTENTION, CacheMode.NO_CACHE,
                    new HttpParams("RequestMethod", "AttentionDel"),
                    new HttpParams("UniqueId", uniqueId),
                    new HttpParams("OtherUniqueId", otherUniqueId));
        } else if (pomPopWinEnum == Constant.POMPopWinEnum.ATTENTION) {
            presenter.requestStrData(2, Interface.INTERFACE_ATTENTION, CacheMode.NO_CACHE,
                    new HttpParams("RequestMethod", "AttentionAdd"),
                    new HttpParams("UniqueId", uniqueId),
                    new HttpParams("OtherUniqueId", otherUniqueId));
        }
    }

    @Override
    public void showOnStart(Request request, int requestId) {
        super.showOnStart(request, requestId);
        loadingDogDialog.show();
    }

    @Override
    public void showOnSuccess(int requestId, String responseBody, Response response) {
        super.showOnSuccess(requestId, responseBody, response);
        if (requestId == 0) {
            RootRes rootRes = gson.fromJson(responseBody, RootRes.class);
            if (rootRes.isResultStatus())
                isAttention = (boolean) rootRes.getResultData();
        } else if (requestId == 1) {
            RootRes rootRes = gson.fromJson(responseBody, RootRes.class);
            ToastUtils.showShort(rootRes.getResultDes());
            if (rootRes.isResultStatus()) {
                isAttention = false;
                pomPopWindow.setAttentionStatue(false);
                EventBusUtils.sendStickyEvent(new Event(EventBusCode.AttentionList_Refresh));
            }
        } else if (requestId == 2) {
            RootRes rootRes = gson.fromJson(responseBody, RootRes.class);
            ToastUtils.showShort(rootRes.getResultDes());
            if (rootRes.isResultStatus()) {
                isAttention = true;
                pomPopWindow.setAttentionStatue(true);
                EventBusUtils.sendStickyEvent(new Event(EventBusCode.AttentionList_Refresh));
            }
        }
    }

    @Override
    public void showOnFinish(int requestId) {
        super.showOnFinish(requestId);
        loadingDogDialog.dismiss();
    }
}
